package com.sdk.es.http.gen;

import cn.hutool.core.util.StrUtil;

import java.util.List;
import java.util.TreeMap;

/**
 * @author limaosheng
 * @create 2023-08-25 15:45
 * @desc
 */
public class GenEntity {
    //是否覆盖原文件
    public static final Boolean isOver = false;

    //需要使用Integer的字段,其它所有es内number类型字段默认使用Long
    public static final List<String> INTEGER_FILER = List.of(
            "communityId");

    public static void main(String[] args) {
        String index = "health_device_track";
        /**
         * kibana-管理-索引模式-创建索引，在页面选中名称和类型,直接复制赋值给STR1，如果字段数超过50，超过50部分赋值给STR2
         * 可注释genEntityAndQuery方法，自行添加实体类，生成其它相关的代码,自添加的实体类要加上id和index
         */
        String str1 = STR1;//前1-50个字段
        String str2 = STR2;//前51-100字段
        //生成entity和entityQuery
        genEntity(index, str1, str2);
    }
    public static final String STR1 = "categoryId\n" +
            "number\n" +
            "\n" +
            "committeeName\n" +
            "string\n" +
            "\n" +
            "committeeName.keyword\n" +
            "string\n" +
            "\n" +
            "communityId\n" +
            "number\n" +
            "\n" +
            "deviceId\n" +
            "number\n" +
            "\n" +
            "deviceName\n" +
            "string\n" +
            "\n" +
            "deviceName.keyword\n" +
            "string\n" +
            "\n" +
            "faceImageId\n" +
            "string\n" +
            "\n" +
            "faceImageId.keyword\n" +
            "string\n" +
            "\n" +
            "gmtCreate\n" +
            "date\n" +
            "\n" +
            "isWarn\n" +
            "number\n" +
            "\n" +
            "latitude\n" +
            "string\n" +
            "\n" +
            "latitude.keyword\n" +
            "string\n" +
            "\n" +
            "location\n" +
            "string\n" +
            "\n" +
            "location.keyword\n" +
            "string\n" +
            "\n" +
            "longitude\n" +
            "string\n" +
            "\n" +
            "longitude.keyword\n" +
            "string\n" +
            "\n" +
            "measureTime\n" +
            "date\n" +
            "\n" +
            "secondCategoryId\n" +
            "number\n" +
            "\n" +
            "secondCategoryName\n" +
            "string\n" +
            "\n" +
            "secondCategoryName.keyword\n" +
            "string\n" +
            "\n" +
            "userCommunityId\n" +
            "number\n" +
            "\n" +
            "userCommunityName\n" +
            "string\n" +
            "\n" +
            "userCommunityName.keyword\n" +
            "string\n" +
            "\n" +
            "userName\n" +
            "string\n" +
            "\n" +
            "userName.keyword\n" +
            "string\n" +
            "\n" +
            "userPhone\n" +
            "string\n" +
            "\n" +
            "userPhone.keyword\n" +
            "string\n" +
            "\n" +
            "warnContent\n" +
            "string\n" +
            "\n" +
            "warnContent.keyword\n" +
            "string\n" +
            "\n" +
            "warnName\n" +
            "string\n" +
            "\n" +
            "warnName.keyword\n" +
            "string\n" +
            "\n" +
            "warnTime\n" +
            "date"
            ;
    public static final String STR2 = "";
    /**
     * 生成entiy和entityQuery
     *
     * @param index
     * @param str1
     * @param str2
     */
    public static void genEntity(String index, String str1, String str2) {
        String[] arr1 = str1.split("\n\n");
        String[] arr2 = str2.split("\n\n");
        TreeMap<String, String> map = new TreeMap<>();
        for (int i = 0; i < arr1.length; i++) {
            if(arr1[i].contains(".keyword")){
                continue;
            }
            String[] oneArr = arr1[i].split("\n");
            map.put(oneArr[0],oneArr[1]);
        }
        if(str2 != ""){
            for (int i = 0; i < arr2.length; i++) {
                if(arr2[i].contains(".keyword")){
                    continue;
                }
                String[] oneArr = arr2[i].split("\n");
                map.put(oneArr[0],oneArr[1]);
            }
        }

        /**
         * entity
         */
        StringBuffer sb = new StringBuffer();
        sb.append("package com.digitallife.community.elasticsearch.entity.saas;\n" +
                "\n" +
                "import com.digitallife.community.elasticsearch.conts.IndexConts;\n" +
                "import lombok.Data;\n" +
                "import org.springframework.data.annotation.Id;\n" +
                "import org.springframework.data.elasticsearch.annotations.Document;\n" +
                "import org.springframework.data.elasticsearch.annotations.IndexedIndexName;" +
                "\n" +
                "@Document(indexName = IndexConts."+index.toUpperCase()+" + \"_#{@indexNameGenerator.getYearMonthStr()}\")\n" +
                "@Data\n" +
                "public class "+StrUtil.upperFirst(StrUtil.toCamelCase(index))+" {\n" +
                "    @Id\n" +
                "    private String id;\n" +
                "    @IndexedIndexName\n" +
                "    private String index;\n");
        for(String key : map.keySet()){
            sb.append("    //\r\n");
            String javaType = esTypeToJavaType(map.get(key),key);
            sb.append("\tprivate " + javaType + " " + key + ";\r\n");
        }
        sb.append("}");
        String entiyPath = System.getProperty("user.dir") + "/common/src/main/java/com/digitallife/community/elasticsearch/gen/entity/"
                + StrUtil.upperFirst(StrUtil.toCamelCase(index)) + ".java";
        FileUtil.write(entiyPath, sb.toString(),isOver);
    }
    private static String esTypeToJavaType(String esType,String key) {
        String javaType = "???";
        if ("number".equals(esType)) {
            javaType = "Long";
        }else if("string".equals(esType)){
            javaType = "String";
        }else if("date".equals(esType)){
            javaType = "LocalDateTime";
        }
        if(INTEGER_FILER.contains(key)){
            javaType = "Integer";
        }
        return javaType;
    }

}
